<?php
/**
 * @file   admin.php
 * @date   2017-11-15 15:22:57
 * @author huwhis<huuwhois>
 * @version  0.0.1
 */

namespace app\admin\model;

use think\Model;

class Category extends Model
{
    protected $table ='dean_category' ;

    public function categoryName()
    {
        return $this->field('*')->select();
    }

    public function categoryChildren()
    {
        $data = $this->categoryName();
        $data = $this->getChildren($data, 0);
        // echo "<pre>";
        // print_r($data);exit;
        return $data;
    }

    public function getChildren($a, $pid)
    {
        $tree = array();                                //每次都声明一个新数组用来放子元素
        foreach ($a as $v) {
            if ($v->pid == $pid) {                      //匹配子记录
                $v->_child = $this->getChildren($a, $v->id); //递归获取子记录
                if ($v->_child == null) {
                    unset($v->_child);             //如果子元素为空则unset()进行删除，说明已经到该分支的最后一个元素了（可选）
                }
                $tree[] = $v;                           //将记录存入新数组
            }
        }
        return $tree;                                  //返回新数组
    }
}
